<?php
/**
 * Created by PhpStorm.
 * User: sunfan
 * Date: 2017/3/22
 * Time: 17:22
 */

namespace Admin\Controller;


use Admin\Model\EnterpriseModel;
use Admin\Model\UserModel;

class CompanyUserController extends AdminController
{

    public function index()
    {
       $model  = new UserModel();
       $list = $model->join('onethink_enterprise on onethink_user.company_id = onethink_enterprise.id')->where(array('onethink_enterprise.admin_id'=>UID))
           ->field('worker_id,onethink_user.name,onethink_user.mobile,section,register_time,
           onethink_user.status, onethink_enterprise.name as company,fuli,onethink_user.image,onethink_user.id')
           ->select();
//       echo "<pre/>";
//        print_r($list);die;
       $this->assign('list',$list);
       $this->display();
    }


    public function add(){
        if(IS_POST){
           $data = $_POST;
           $model = new UserModel();
           if($model->create()){
              $data['login_pass'] = md5($data['login_pass']);
             if($model->add($data)){
                $this->success('添加会员成功！',U('CompanyUser/index'));
             }else{
                 $this->error("添加失败，请稍后再试!");
               }
           }else{
               $this->error($model->getError());
           }
        }
        $model = new EnterpriseModel();
        $company = $model->where(array('admin_id'=>UID))->field('id,name')->find();
        $this->assign('company',$company);
        $this->display();
    }

    public function edit()
    {
       if(IS_POST){
          $data = $_POST;
           $model  = new UserModel();
          if(!$model->create()){
              $this->error($model->getError());
          }
           $uid = $data['id'];
           if($data['image']){
             $unlink = $model->where(array('id'=>$uid))->getField('image');
             unlink($unlink);
           }else{
            unset($data['image']);
           }
          $data['login_pass'] = md5($data['login_pass']);
          if($model->where(array('id'=>$uid))->save($data)){
             $this->success("编辑成功!",U('CompanyUser/index'));
          }else{
             $this->error("编辑失败，请稍后再试!");
          }
       }
      $uid = I('get.uid');
      $model  = new UserModel();
        $user = $model->join('onethink_enterprise on onethink_user.company_id = onethink_enterprise.id')->where(array('onethink_enterprise.admin_id'=>UID))
            ->field('worker_id,onethink_user.name,onethink_user.mobile,section,register_time,onethink_enterprise.name as company,
            onethink_user.login_user,fuli,onethink_user.image,onethink_user.id,onethink_user.company_id')
            ->where(array('onethink_user.id'=>$uid))
            ->find();
      $this->assign('data',$user);
      $this->display();
    }

    public function delete()
    {
        $id = I("get.uid");
        if($id>0){
            $model  = new UserModel();
            $unlink = $model->where(array('id'=>$id))->getField('image');
            unlink($unlink);
            if($model->where(array('id'=>$id))->delete())
            {
               $this->success("删除成功!",U('CompanyUser/index'));
            }else{
               $this->error('删除失败,请稍后再试!');
            }
        }
    }

    //

    public function enable()
    {
        if(IS_POST){
            if(!I('post.ids')){
                $this->error("请选择一个或多个员工!");
            }
            $model = new UserModel();
            foreach (I('post.ids') as $v)
            {
                if($v>0){
                    $model->where('id='.$v)->save(array('status'=>1));
                }
            }
            $this->success('以上员工已启用',U('CompanyUser/index'));
        }
    }

    public function disable()
    {
        if(IS_POST){
            if(!I('post.ids')){
                $this->error("请选择一个或多个员工!");
            }
            $model = new UserModel();
            foreach (I('post.ids') as $v)
            {
                if($v>0){
                    $model->where('id='.$v)->save(array('status'=>2));
                }
            }
        }
        $this->success('以上员工已禁用',U('CompanyUser/index'));
    }

    public function clear(){
        if(IS_POST){
            if(!I('post.ids')){
                $this->error("请选择一个或多个员工!");
            }
            $model = new UserModel();
            foreach (I('post.ids') as $v)
            {
                if($v>0){
                    $unlink = $model->where(array('id'=>$v))->getField('image');
                    unlink($unlink);
                    $model->where('id='.$v)->delete();
                }
            }
        }
        $this->success('以上员工已删除',U('CompanyUser/index'));
    }


}